package persistance.dal;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import ucc.objects.ContenanceDTO;
import biz.BizFactory;

public class ContenanceFinderImpl implements ContenanceFinder {

	private PreparedStatement q1;
	private PreparedStatement q2;
	private final String Q1 = "select * from caisse.contenances where id_contenance=?";
	private final String Q2 = "select * from caisse.contenances";

	public ContenanceFinderImpl() {
		this.q1 = DalBackEndServices.INSTANCE.prepare(Q1);
		this.q2 = DalBackEndServices.INSTANCE.prepare(Q2);
	}

	public ContenanceDTO laContenanceNum(int id) {
		ResultSet rs = null;
		try {
			q1.setInt(1, id);
			rs = q1.executeQuery();
			ContenanceDTO contenance = BizFactory.INSTANCE
					.createEmptyContenanceDTO();
			while (rs.next()) {
				contenance.setId(id);
				contenance.setQuantite(rs.getDouble("quantite"));
				contenance.setUnite(rs.getString("unite"));
			}
			return contenance;
		} catch (SQLException e) {
			System.out.println("[E][SQL] : ");
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<ContenanceDTO> lesContenances() {
		ResultSet rs = null;
		ArrayList<ContenanceDTO> listeContenances = new ArrayList<ContenanceDTO>();
		try {
			rs = q2.executeQuery();
			while (rs.next()) {
				ContenanceDTO contenance = BizFactory.INSTANCE
						.createEmptyContenanceDTO();

				contenance.setId(rs.getInt("id_contenance"));
				contenance.setQuantite(rs.getDouble("quantite"));
				contenance.setUnite(rs.getString("unite"));

				listeContenances.add(contenance);
			}
			return listeContenances;
		} catch (SQLException e) {
			System.out.println("[E][SQL] : ");
			e.printStackTrace();
		}
		return null;
	}
}
